function ccrUnit(unit)
	local obj_to_unit
	-- Convert GUID to object
	if type(unit) == "string"
	and string.len(unit) == 18
	and string.sub(unit, 1, 2) == "0x"
	then
		unit = GetObjectFromGUID(unit)
	end

	if type(unit) == "table" then
		-- FireHack Object to UnitID
		ClearUnitID("ccR_Unit")
		unit:SetAsUnitID("ccR_Unit")
		obj_to_unit = "ccR_Unit"
	end

	-- Neither object or guid, pass it as a normal string
	if obj_to_unit == nil then obj_to_unit = unit end
	return obj_to_unit
end
function ccrUnitToObject(unit)
	local objUnit
    if unit == nil then objUnit = Target end
    if type(unit) == "string" then objUnit = GetObjectFromUnitID(unit) end
    if type(unit) == "table" then objUnit = unit end
    return objUnit
end
function ccrUnitMovingSpeed(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		return GetUnitSpeed(unit)
	end
end
function ccrUnitHealth(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		return (UnitHealth(unit) / UnitHealthMax(unit) * 100)
	else
		return 0
	end
end
function ccrUnitName(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		return GetUnitName(unit)
	end
end
function ccrUnitLevel(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		return UnitLevel(unit)
	end
end
function ccrUnitTarget(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		if unit == "FHUnit" then
			if FHUnit and FHUnit:Exists() and not FHUnit:IsDead() then
				local vName = GetObjectFromUnitID("FHUnit"):GetTarget():GetName()
				if vName ~= nil then
					return vName
				end
			end
		else
			local vName, vRealm = UnitName(unit .. "Target")
			return vName
		end
	end
end
function ccrUnitRace(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and UnitIsPlayer(unit) == 1 then
		local vRace = {UnitRace(unit)}
		if vRace[1] ~= nil then
			return vRace[1]
		end
	end
end
function ccrUnitSpec(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and UnitIsPlayer(unit) == 1 then
		local vSpec = GetSpecialization(unit)
		return vSpec
	end
end
function ccrUnitClass(unit)
	local unit = ccrUnit(unit)
	-- by Sheltering
	local tmp = select(3, UnitClass(unit))
	if tmp==0 then return "None"
	elseif tmp==1 then return "Warrior"
	elseif tmp==2 then return "Paladin"
	elseif tmp==3 then return "Hunter"
	elseif tmp==4 then return "Rogue"
	elseif tmp==5 then return "Priest"
	elseif tmp==6 then return "Death Knight"
	elseif tmp==7 then return "Shaman"
	elseif tmp==8 then return "Mage"
	elseif tmp==9 then return "Warlock"
	elseif tmp==10 then return "Monk"
	elseif tmp==11 then return "Druid"
	end
end
function ccrUnitMana(unit)
	local unit = ccrUnit(unit)
	return UnitPower(unit)/UnitPowerMax(unit) * 100
end
function ccrUnitHasBuff(unit, buff)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and buff ~= nil then
		-- Check for Spell ID
		if tonumber(buff) == nil then
			-- Spell name has been used
			local vBuff = {UnitBuff(unit, buff)}
			if vBuff[1] == buff then
				return true
			else
				return false
			end			
		else
			-- Convert the SpellID to it's name
			local vBuffName = {GetSpellInfo(buff)}
			-- Invalid SpellID used
			if vBuffName[1] == nil then 
				return false
			end
			-- Use the name to see if the unit has it
			local vBuff = {UnitBuff(unit, vBuffName[1])}
			-- If they match, Win.
			if vBuff[1] == vBuffName[1] then
				return true
			else
				return false
			end
		end
	end
end
function ccrUnitHasMyBuff(unit, buff)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and buff ~= nil then
		local vBuff = {UnitBuff(unit, buff)}
		if vBuff[1] == buff and vBuff[8] == "player" then
			return true
		else
			return false
		end	
	end
end
function ccrUnitHasBuffRemaining(unit, buff, time)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and buff ~= nil and time ~= nil then
		if tonumber(buff) ~= nil then buff = SpellIDToName(buff) end
		if ccrUnitHasBuff(unit, buff) then
			local vBuff = {UnitBuff(unit, buff)}
			local vExpires = vBuff[7]
			local vCurrentTime = GetTime()
			if (vExpires-vCurrentTime) <= time then
				return true
			else
				return false
			end
		else
			return false
		end
	end
end
function ccrUnitBuffDuration(unit, buff)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and buff ~= nil then
		if tonumber(buff) ~= nil then buff = SpellIDToName(buff) end
		if ccrUnitHasBuff(unit, buff) then
			local vBuff = {UnitBuff(unit, buff)}
			local vExpires = vBuff[7]
			local vCurrentTime = GetTime()
			return (vExpires-vCurrentTime)
		end
	end
end
function ccrUnitHasBuffStacks(unit, buff, stacks)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and buff ~= nil and stacks ~= nil then
		if tonumber(buff) ~= nil then buff = SpellIDToName(buff) end
		if ccrUnitHasBuff(unit, buff) then
			local vBuff = {UnitBuff(unit, buff)}
			local vStacks = vBuff[4]
			if vStacks >= stacks then
				return true
			else
				return false
			end
		else
			return false
		end
	end	
end
function ccrUnitHasBuffStacksLeft(unit, buff, stacks)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and buff ~= nil and stacks ~= nil then
		if ccrUnitHasBuff(unit, buff) then
			local vBuff = {UnitBuff(unit, buff)}
			local vStacks = vBuff[4]
			if vStacks <= stacks then
				return true
			else
				return false
			end
		else
			return false
		end
	end	
end
---
function ccrUnitHasDebuff(unit, debuff)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff then
			return true
		else
			return false
		end
	end	
end
function ccrUnitHasDebuffRemaining(unit, debuff, time)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil and time ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff then
			local vExpires = vDebuff[7]
			local vCurrentTime = GetTime()
			if (vExpires-vCurrentTime) <= time then
				return true
			else
				return false
			end
		else
			return false
		end
	end	
end
function ccrUnitHasDebuffDuration(unit, debuff)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff then
			local vExpires = vDebuff[7]
			local vCurrentTime = GetTime()
			return (vExpires-vCurrentTime)
		else
			return false
		end
	end	
end
function ccrUnitHasDebuffStacks(unit, debuff, stacks)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil and stacks ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff then
			local vStacks = vDebuff[4]
			if vStacks >= stacks then
				return true
			else
				return false
			end
		else
			return false
		end
	end	
end
---
function ccrUnitHasMyDebuff(unit, debuff)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff and vDebuff[8]=="player" then
			return true
		else
			return false
		end
	end	
end
function ccrUnitHasMyDebuffRemaining(unit, debuff, time)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil and time ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff and vDebuff[8]=="player" then
			local vExpires = vDebuff[7]
			local vCurrentTime = GetTime()
			if (vExpires-vCurrentTime) <= time then
				return true
			else
				return false
			end
		else
			return false
		end
	end	
end
function ccrUnitHasMyDebuffDuration(unit, debuff)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff and vDebuff[8]=="player" then
			local vExpires = vDebuff[7]
			local vCurrentTime = GetTime()
			return (vExpires-vCurrentTime)
		else
			return 0
		end
	end	
end
function ccrUnitHasMyDebuffStacks(unit, debuff, stacks)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 and debuff ~= nil and stacks ~= nil then
		if tonumber(debuff) ~= nil then debuff = SpellIDToName(debuff) end
		local vDebuff = {UnitDebuff(unit, debuff)}
		if vDebuff[1] ~= nil and vDebuff[1] == debuff and vDebuff[8]=="player" then
			local vStacks = vDebuff[4]
			if vStacks >= stacks then
				return true
			else
				return false
			end
		else
			return false
		end
	end	
end
---
function ccrUnitIsNormal(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vClassification = UnitClassification(unit)
		if vClassification == "Normal" then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsElite(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vClassification = UnitClassification(unit)
		if vClassification == "elite"
		or vClassification == "rare"
		or vClassification == "rareelite"
		then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsEliteOrAbove(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		vClassification = UnitClassification(unit)
		if vClassification == "elite"
		or vClassification == "rare"
		or vClassification == "rareelite"
		or vClassification == "worldboss"
		or vClassification == ccrUnitIsBoss(unit)
		then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsRare(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		vClassification = UnitClassification(unit)
		if vClassification == "rare"
		or vClassification == "rareelite"
		then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsBoss(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		for i=1, # BossList do
			if BossList[i]==UnitName(unit) then
				return true;
			end
		end
		return false;
	end
	return false;
end
function ccrUnitIsEndBoss(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		for i=1, # EndBossList do
			if EndBossList[i]==ccrUnitName(unit) then
				return true;
			end
		end
		return false;
	end
end
function ccrUnitIsMoving(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vUnitSpeed = GetUnitSpeed(unit)
		if vUnitSpeed > 0 then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsFacingMe(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1
	and Target and Target:Exists() and not Target:IsDead()
	and Player ~= nil
	then
		if Target:IsFacing(Player) then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsCasting(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vCast = {UnitCastingInfo(unit)}
		if vCast[1] ~= nil then
			return true
		else
			return false
		end
	else
		return unit .. " Does not exist"
	end
end
function ccrUnitIsCastingSpell(unit, spell)
	local unit = ccrUnit(unit)
	if tonumber(spell) ~= nil then spell = SpellIDToName(spell) end
	if UnitExists(unit) == 1 then
		local vCast = {UnitCastingInfo(unit)}
		if vCast[1] ~= nil
		and vCast[1] == spell
		then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsCastingAtMe(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vCast = {UnitCastingInfo(unit)}
		if vCast[1] ~= nil then
			if ccrUnitTarget(unit) == ccrPlayerName() then
				return true
			else
				return false
			end
		else
			return false
		end
	else
		return unit .. "Does not exist"
	end
end
function ccrUnitIsChanneling(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vChannel = {UnitChannelInfo(unit)}
		if vChannel[1] ~= nil then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsChannelingSpell(unit, spell)
	local unit = ccrUnit(unit)
	if tonumber(spell) ~= nil then spell = SpellIDToName(spell) end
	if UnitExists(unit) == 1 then
		local vChannel = {UnitChannelInfo(unit)}
		if vChannel[1] ~= nil
		and vChannel[1] == spell
		then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsChannelingAtMe(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vChannel = {UnitChannelInfo(unit)}
		if vChannel[1] ~= nil then
			if ccrUnitTarget(unit) == ccrPlayerName() then
				return true
			else
				return false
			end
		end
	end
end
function ccrUnitIsCastingOrChanneling(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vCast = {UnitCastingInfo(unit)}
		local vChannel = {UnitChannelInfo(unit)}
		if vCast[1] ~= nil or vChannel[1] ~= nil then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsInteruptable(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		if ccrUnitIsCasting(unit) then
			local vCast = {UnitCastingInfo(unit)}
			if vCast[9] == false then return true else return false end
		end
		if ccrUnitIsChanneling(unit) then
			local vChannel = {UnitChannelInfo(unit)}
			if vChannel[8] == false then return true else return false end
		end
	end
end	
function ccrUnitHasLeftOnCast(unit, time)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vCast={UnitCastingInfo(unit)}
		if vCast[1] ~= nil then
			local vFinish = (vCast[6]/1000) - GetTime()
			--PrintToChat("Time:" .. time .. " Finish: " .. vFinish)
			if vFinish <= time then
				return true;
			else
				return false;
			end
		else
			return false
		end
	end
end
function ccrUnitHasLeftOnChannel(unit, time)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vChannel={UnitChannelInfo(unit)}
		if vChannel[1] ~= nil then
			local vFinish = (vChannel[6]/1000) - GetTime() 
			if time >= vFinish then
				return true;
			else
				return false;
			end
		end
	end
end
function ccrUnitIsAlive(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		if UnitIsDead(unit) == nil then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsDead(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		if UnitIsDead(unit) == 1 then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsAfterMe(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		if ccrUnitTarget(unit) == ccrPlayerName() then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsWithinMeleeRange(unit)
	local objUnit
    if unit == nil then objUnit = Target end
    if type(unit) == "string" then objUnit = GetObjectFromUnitID(unit) end
    if type(unit) == "table" then objUnit = unit end
    if objUnit
    and objUnit:Exists()
    then
		local vDistance = objUnit:GetDistance()
		if vDistance <= max(5, (13 / 6) + objUnit:GetCombatReach()) then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsWithinRanged(unit)
	if UnitExists(unit) == 1 then
		local objTarget = GetObjectFromUnitID(unit)
		if objTarget and objTarget:Exists() then
			local vDistance = objTarget:GetDistance()
			if vDistance <= 44 then
				return true
			else
				return false
			end
		end
	end
end
function ccrUnitIsWithinDistance(unit, distance)
	if UnitExists(unit) == 1 then
		local objTarget = GetObjectFromUnitID(unit)
		local vDistance = objTarget:GetDistance()
		if vDistance > 0 
		and vDistance <= distance then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsAPlayer(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		if UnitIsPlayer(unit) == 1 then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsEnemyPlayer(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vEnemy = UnitIsEnemy("Player", unit)
		local vPlayer = UnitIsPlayer(unit)
		local vAttackable = UnitCanAttack("Player", unit)
		if vEnemy == 1
		and vPlayer == 1
		and vAttackable == 1
		then
			return true
		else
			return false
		end
	end	
end
function ccrUnitIsTank(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vDungeonRole = UnitGroupRolesAssigned(unit)
		if vDungeonRole == "TANK" then return true end;
	end
end
function ccrUnitIsHealer(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vDungeonRole = UnitGroupRolesAssigned(unit)
		if vDungeonRole == "HEALER" then return true end;
	end
end
function ccrUnitIsDPS(unit)
	local unit = ccrUnit(unit)
	if UnitExists(unit) == 1 then
		local vDungeonRole = UnitGroupRolesAssigned(unit)
		if vDungeonRole == "DAMAGER" then return true end;
	end
end
function ccrUnitIsInLineOfSight(unit)
	if UnitExists(unit) == 1 then
		local objTarget = GetObjectFromUnitID(unit)
		if objTarget
		and objTarget:Exists()
		and not objTarget:IsDead()
		and objTarget:InLineOfSight() then
			return true
		else
			return false
		end
	end
end
function ccrUnitIsInCombat(unit)
	if UnitAffectingCombat(unit) == 1 then
		return true
	else
		return false
	end
end